<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '管理员添加' : '管理员编辑'"
      :visible.sync="obj.isTrue"
      width="40%"
      @close="cancel()">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="所属角色">
          <el-select v-model="form.roleid" placeholder="请选择所属角色">
            <el-option label="请选择" :value="0"></el-option>
            <el-option v-for="item in roleList" :key="item.id" :label="item.rolename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="add()" v-if="obj.isAdd">编 辑</el-button>
        <el-button type="primary" @click="enterEdit()" v-else>删 除</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { manageAdd, manageInfo, manageEdit } from "../../../request/api";
import { successAlert, warningAlert } from "../../../utils/alert";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      form: {
        roleid: 0,
        username: "",
        password: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      roleList:"role/list"
    }),
  },
  methods: {
    ...mapActions({
      asyncRoleList:"role/asyncList",
      asyncManageList:"manage/asyncList",
      asyncManageCount: "manage/asyncCount"
    }),
    // 清空
    empty() {
      this.form = {
        roleid: 0,
        username: "",
        password: "",
        status: 1,
      };
    },
    // 点击取消
    cancel() {
      // 1、清空form中的数据
      this.empty();
      // 2、关闭遮罩层
      this.obj.isTrue = false;
    },
    // 点击添加
    add() {
      console.log(this.form);
      manageAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          // 添加成功
          successAlert(res.data.msg);
          // 清空form并关闭对话框
          this.cancel();
          // 刷新manage的列表数据
          this.asyncManageList()
          // 重新请求manage的列表总数
          this.asyncManageCount()
        } else {
          // 添加失败
          warningAlert(res.data.msg);
        }
      });
    },
    // 获取一条信息
    getOne(uid) {
      manageInfo({ uid: uid }).then((res) => {
        // 给form赋值
        this.form = res.data.list;
        // 置空密码框
        this.form.password = ""
      });
    },
    // 确定编辑
    enterEdit() {
      manageEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          // 成功提示窗
          successAlert(res.data.msg);
          // 关闭当前遮罩层并清空数据
          this.cancel();
          // 重新请求列表数据
          this.asyncManageList();
        } else {
          // 失败提示窗
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {
    // 页面一进来请求角色列表数据
    this.asyncRoleList()
  },
};
</script>
<style scoped>
</style>